package com.web;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.domain.Address;
import com.service.AddresService;
import com.service.Impl.AddresServiceImpl;

@WebServlet("/phoneService")
public class PhoneService extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取Session对象
        HttpSession session = request.getSession();
        //1.获取手机框输入的数据
        String phone = request.getParameter("phone");
        //2.判断输入的收据是否存在于数据库中
        if(phone.matches("^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$")) {
            //创建对象调用方法
            AddresService addresService = new AddresServiceImpl();
            List<Address> list = addresService.findAddress();
            //判断手机号是否存在于库中
            for(Address address : list) {
                System.out.println("已被注册");
                if(phone.equals(address.getTelphone())) {
                    //如果库中存在此电话号码
                    session = request.getSession();
                    session.setAttribute("messge", "温馨提示:您输入的手机号已被注册~~");
                    request.getRequestDispatcher("/page/login2.jsp").forward(request, response);
                    return;
                }else {
                    System.out.println("跳转下一个页面");
                    //没有存在就调往下一个注册页面
                    request.getRequestDispatcher("/page/login3.jsp").forward(request, response);
                    return;
                }
            }
		}else {
            session = request.getSession();
            session.setAttribute("messge", "温馨提示:您输入的手机号不合格或未输入~~");
            request.getRequestDispatcher("/page/login2.jsp").forward(request, response);
        }




        //创建实现类对象
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
}
